package com.mars.mongodemo.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author by: mars
 * Date: 2020/3/31 13:46
 * Description:
 */
@RestController
@RequestMapping("/mongo")
public class UserController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;

    @GetMapping("save")
    public String save(){
        UserInfo userInfo = new UserInfo(System.currentTimeMillis(),"summer","33333");
        //userRepository.save(userInfo);
        // mongoTemplate
        userService.saveObj(userInfo);
        return "ok";
    }

    @GetMapping("getUserList")
    public List<UserInfo> getUserList(){
        // mongoTemplate
        return userService.findAll();
        //List<UserInfo> userInfoList = userRepository.findAll();
        //return userInfoList;
    }

    @GetMapping("update")
    public String update(Long id,String username,String password){
        UserInfo userInfo = new UserInfo(id,username,password);
        // mongoTemplate
        userService.updateUserInfo(userInfo);
        //userRepository.save(userInfo);
        return "success";
    }

    @GetMapping("delete")
    public String delete(Long id,String username,String password){
        UserInfo userInfo = new UserInfo(id,username,password);
        // mongoTemplate
        userService.deleteUserInfo(userInfo);
        return "success";
    }
}
